<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * 后台管理员登录日志管理模型类
 * @author 66diqiu-li
 *
 */
class Admin_Adminloginlog_model extends CI_Model {

    /**
     * @var 表名称
     */
    private $table;
    private $sid;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->table  = $this->db->dbprefix("admin_user_login_log");
    }

    /**
    * 添加数据
    * @param $data
    * @return bool
    */
    function add_row($data){
        if(is_array($data)){
            if($this->db->insert($this->table,$data)){
                return $this->db->insert_id();
            }else{
                return false;
            }
        }
    }
	
    /**
     *
     * 删除数据
     * @param array $where 删除条件
     */
    function remove_row($where=array()){
        $where = $this->set_where($where);
        $this->db->where($where, NULL, FALSE);
        return $this->db->delete($this->table);
    }

    /**
     * 获取所有管理管理员登录日志
     * @param int $page_size 每页显示数量
     * @param int $now_page 第几页
     * @param $where 选择条件
     * @return array
     */
    function get_list($page_size=20,$now_page=1,$where){
        $where = $this->set_where($where);
        $this->db->select('id,username,login_time,login_ip,status');
        $this->db->order_by('login_time','DESC');
        $this->db->where($where, NULL, FALSE);
        $query = $this->db->get($this->table,$page_size,($now_page-1)*$page_size);
        return $query->result_array();
    }
	
    /**
     *
     * 删除一个月前登录日志
     */
    function deleteAMonthago(){
        $where = 'login_time<='.date('Y-m-d H:i:s',now()-(60*60*24*30));
        $where = $this->set_where($where);
        return $this->db->delete($this->table);
    }

    /**
     *
     * 获取总数量
     * @param array $where 查询条件
     */
    function get_count($where=array()){
        $where = $this->set_where($where);
        $this->db->select('id');
        $this->db->where($where, NULL, FALSE);
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    /**
     * 返回查询条件
     * @param $where 查询条件
     * @return array|string
     */
    function set_where($where){
        if(is_array($where))$where['sid']=$this->sid;
        else $where=$where?$where.' AND sid='.$this->sid:'sid='.$this->sid;
        return $where;
    }
}